import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 131. 分割回文串
 * @author: WXY
 * @create: 2023-01-19 09:02
 * @Version 1.0
 **/
public class Num131_partition {
    public List<List<String>> ans = new ArrayList<>();
    public List<String> path = new ArrayList<>();
    public List<List<String>> partition(String s) {
        char[] str = s.toCharArray();
        process(s, 0);
        return ans;
    }

    private void process(String str, int index) {
        if (index >= str.length()) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < str.length(); i++) {
            if (isPalindrome(str, index, i)) {
                String cur = str.substring(index, i - index + 1);
                path.add(cur);
            } else {
                continue;
            }
            process(str, i + 1);
            path.remove(path.size() - 1);
        }
    }

    private boolean isPalindrome(String str, int l, int r) {
        while (l < r) {
            if (str.charAt(l) != str.charAt(r)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}
